<?php
/**
 * Created by PhpStorm.
 * User: 73640
 * Date: 2018/11/12
 * Time: 14:03
 */

namespace App\Service;


use App\Exceptions\ApiException;

class DesService
{
    /**
     * @var string
     */
    var $key = '';
    /**
     * @var string
     */
    var $iv = '';
    /**
     * Des3 constructor.
     * @param string|null $key
     * @param string|null $iv
     */
    public function __construct()
    {
        $key=config('tele.key');
        if (strlen($key) != 24){
            throw new ApiException("DES3_KEY长度错误，长度为24");
        }
        $this->key = $key;
    }
    public function encrypt($input){
        return base64_encode(openssl_encrypt($input, "des-ede3", $this->key, OPENSSL_RAW_DATA));
    }
    public function decrypt($encrypted){
        return openssl_decrypt(base64_decode($encrypted), 'des-ede3', $this->key, OPENSSL_RAW_DATA);
    }
}